#!/bin/bash

SMX_DIR="/pbfd/manageability/smx/smx_trunk"
# http://dmtf.org/standards/cim
CIM_NEW="/pbfd/manageability/smx/dmtf/cim_schema_2.36.0Final-MOFs"
WBEM_TC="/pbfd/manageability/smx/wbem-tc"
TGT_DIR="/pbfd/www/lansvr/smx"
WORKING_DIR="`dirname $0`"

[ ! -d "$SMX_DIR" ] && exit 1
[ ! -d "$CIM_NEW" ] && exit 1
[ ! -d "$WBEM_TC" ] && exit 1
[ ! -d "$TGT_DIR" ] && exit 1

function copy_mof()
{
  [ "$1" == "" ] && return -1
  [ "$2" == "" ] && return -1
  [ ! -f "$1" ] && return -1

  # echo "copy_mof $1 -> $2"
  dos2unix -n "$1" "$2.tmp" 1>/dev/null 2>&1
  cat "$2.tmp" | awk 'BEGIN{PAD="";add_public=0;PRE="";}{\
    if ( add_public == 1 ) { \
      PRE="public:";\
      add_public=0;\
    }\
    if ( $0 ~ /^[ \t]*\[/ && $0 !~ /^[ \t]*\[IN\]/) {\
      gsub(/^[ \t]*\[/, "/**", $0);\
      PAD="\\n";\
    }\
    if ( $0 ~ /\][ \t]*$/) {\
      gsub(/\][ \t]*$/, "*/", $0);\
      PAD="";\
      add_public=1;\
    }\
    gsub(/\\n/,"",$0);\
    gsub(/\\/,"",$0);\
    print PRE,$0,PAD;\
    PRE="";\
  }' > $2
  rm -f "$2.tmp"
  return 0
}

function rm_space()
{
  echo "$1" | awk '{gsub(" ", "_", $0); print $0}'
}

function copy_dir()
{
  [ "$1" == "" ] && return -1
  [ "$2" == "" ] && return -1
  SRCD="$1"
  TGTD="$2/_mofs"
  echo "$1 -> $2"
  [ ! -d "$TGTD" ] && mkdir -p $TGTD

  find $SRCD -name "*.mof" | while read moffn
  do
    mfnm="`basename "$moffn"`"
    tgtnm="`rm_space "$mfnm"`"
    copy_mof "$moffn" "$TGTD/$tgtnm"
  done

  return 0
}

function handle_smx_zip()
{
  mkdir $TGT_DIR/temp

  cp -f $SMX_DIR/common/mofs/cimv2340Final-MOFs.tar $TGT_DIR/temp
  cur_dir=`pwd`
  cd $TGT_DIR/temp
  tar -xf cimv2340Final-MOFs.tar
  rm -f cimv2340Final-MOFs.tar
  cd $cur_dir

  copy_dir "$TGT_DIR/temp/Experimental" "$TGT_DIR/exps"
  rm -rf "$TGT_DIR/temp/Experimental"

  copy_dir "$TGT_DIR/temp" "$TGT_DIR/mofs"
  rm -rf "$TGT_DIR/temp"
}

function remove_smx_junk(){
  rm -f $TGT_DIR/mofs/_mofs/cim*
  rm -f $TGT_DIR/mofs/_mofs/hp*
  rm -f $TGT_DIR/mofs/_mofs/m*
  rm -f $TGT_DIR/mofs/_mofs/p*
  rm -f $TGT_DIR/mofs/_mofs/q*
  rm -f $TGT_DIR/mofs/_mofs/M001*
  rm -f $TGT_DIR/mofs/_mofs/B*
  rm -f $TGT_DIR/mofs/_mofs/E*
  rm -f $TGT_DIR/mofs/_mofs/QualifierTest.mof
  rm -f $TGT_DIR/mofs/_mofs/SMXQualifiers.mof
  rm -f $TGT_DIR/mofs/_mofs/HP_Power_1.3.0.mof
}

function do_doxygen()
{
  PJN="$1"
  TGTD="$2"

  cat "$WORKING_DIR/Doxyfile" | awk 'BEGIN{PJN="'"$PJN"'"}{\
   if ($1 == "PROJECT_NAME") {\
    print "PROJECT_NAME           = \""PJN"\"";\
   } else {\
    print $0;}\
  }' > "$TGTD/Doxyfile"

  echo "Done, runing doxygen in \"$TGTD\" ......"
  cur_dir="`pwd`"
  cd "$TGTD"
  doxygen >/dev/null 2>&1
  cd "$cur_dir"
}

function sync_smx()
{
  rm -rf "$TGT_DIR/mofs"
  rm -rf "$TGT_DIR/exps"

  copy_dir "$SMX_DIR" "$TGT_DIR/mofs"

  handle_smx_zip
  remove_smx_junk

  do_doxygen "Server Management eXtenstions Profiles" "$TGT_DIR/mofs"
  do_doxygen "DMTF Experimental Profiles" "$TGT_DIR/exps"
}

function sync_latest()
{
  rm -rf "$TGT_DIR/latest"

  copy_dir "$CIM_NEW" "$TGT_DIR/latest"
  copy_dir "$WBEM_TC" "$TGT_DIR/latest"

  do_doxygen "Latest DMTF & HP WBEM-TC Profiles" "$TGT_DIR/latest"
}

sync_smx
sync_latest

